package com.hefei

import org.apache.spark.rdd.RDD
import org.apache.spark.{SparkConf, SparkContext}

/**
 * @author caizhengjun
 * @date 2020/8/27 8:28 下午
 */
object WordCount2 {
  def main(args: Array[String]): Unit = {

    val conf: SparkConf = new SparkConf().setMaster("local[2]").setAppName(this.getClass.getSimpleName)

    val sc: SparkContext = new SparkContext(conf)

    val source: RDD[String] = sc.textFile("hdfs://work01:9000/user/caizhengjun/input")

    //    val flatten: RDD[String] = source.flatMap(_.split(" "))
    val flatten: RDD[String] = source.flatMap(_.split(" "))

    //flatten.foreach(println(_))

    //    val wordTuple: RDD[(String, Int)] = flatten.map((_, 1))
    val wordTuple: RDD[(String, Int)] = flatten.map((_, 1))

    //wordTuple.foreach(println(_))

    //    val reduceWord: RDD[(String, Int)] = wordTuple.reduceByKey(_ + _)
    val reduceWord: RDD[(String, Int)] = wordTuple.reduceByKey(_ + _)

    reduceWord.foreach(println(_))
    sc.stop()
  }
}
